package com.happycat.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.happycat.Pagination;
import com.happycat.ResponseHeaderModel;
import com.happycat.ResponseModel;
import com.happycat.SysContants;
import com.happycat.mapper.UserPunchClockMapper;
import com.happycat.mobile.model.UserAccount;
import com.happycat.mobile.model.UserLightStack;
import com.happycat.mobile.model.UserPunchClock;
import com.happycat.mobile.service.GenerateIdService;
import com.happycat.mobile.service.UserAccountService;
import com.happycat.mobile.service.UserLightStackService;
import com.happycat.mobile.service.UserPunchClockService;
import com.happycat.utils.DateUtil;

/**
 * Module: UserPunchClock.java
 * 
 * @author liaoyong
 * @since JDK 1.8
 * @version 1.0
 * @date 2020-08-01
 * @Descriptions:
 */
@Service
public class UserPunchClockServiceImpl extends ServiceImpl<UserPunchClockMapper, UserPunchClock>
		implements UserPunchClockService {

	@Autowired
	private UserAccountService userAccountService;
	@Autowired
	private UserLightStackService userLightStackService;
	@Autowired
	private GenerateIdService generateIdService;

	public ResponseModel<UserPunchClock> selectOne(UserPunchClock userPunchClock) {
		Wrapper<UserPunchClock> queryWrapper = new QueryWrapper<>(userPunchClock);
		ResponseModel<UserPunchClock> responseModel = new ResponseModel<>();
		responseModel.setCode(SysContants.SYS_SUCCESS_CODE);
		responseModel.setMessage("查询成功");
		responseModel.setData(getOne(queryWrapper));
		return responseModel;

	}

	public ResponseHeaderModel add(UserPunchClock userPunchClock) {
		// 查询本月签到次数
		QueryWrapper<UserPunchClock> queryWrapper = new QueryWrapper<UserPunchClock>();
		queryWrapper.gt("create_time", DateUtil.parseStrToDate(DateUtil.getCurrentMonthFirstDay(),
				DateUtil.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS));
		queryWrapper.lt("create_time", DateUtil.parseStrToDate(DateUtil.getCurrentMonthLastDay(),
				DateUtil.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS));
		List<UserPunchClock> list = list(queryWrapper);
		int score = 0;
		if (list != null) {
			if (list.size() <= 3 && list.size() >= 0) {
				score = 2;
			} else if (list.size() <= 5 && list.size() >= 3) {
				score = 3;
			} else if (list.size() <= 14 && list.size() >= 6) {
				score = 4;
			} else if (list.size() <= 29 && list.size() >= 15) {
				score = 6;
			} else if (list.size() >= 30) {
				score = 10;
			}
		} else {
			score = 2;
		}
		UserAccount userAccountResult = userAccountService.getById(userPunchClock.getAccountId());
		UserAccount userAccount = new UserAccount();
		userAccount.setId(userAccountResult.getId());
		userAccount.setLightStick(userAccountResult.getLightStick() + score);
		userAccountService.updateById(userAccount);
		ResponseHeaderModel responseHeaderModel = new ResponseHeaderModel();
		//添加荧光棒明细
		UserLightStack userLightStack = new UserLightStack();
		userLightStack.setAccountId(userPunchClock.getAccountId());
		userLightStack.setChangeTime(new Date());
		userLightStack.setId(generateIdService.nextDefaultSerialNumber());
		userLightStack.setSroce(score);
		userLightStack.setType(SysContants.OPERATION_TYPE_PLUS);
		userLightStackService.save(userLightStack);
		userPunchClock.setId(generateIdService.nextDefaultSerialNumber());
		boolean result = save(userPunchClock);
		if (result) {
			responseHeaderModel.setCode(SysContants.SYS_SUCCESS_CODE);
			responseHeaderModel.setMessage("签到成功");
		} else {
			responseHeaderModel.setCode(SysContants.SYS_FAIL_CODE);
			responseHeaderModel.setMessage("签到失败");
		}
		return responseHeaderModel;
	}

	public ResponseHeaderModel modify(UserPunchClock userPunchClock) {
		ResponseHeaderModel responseHeaderModel = new ResponseHeaderModel();

		boolean result = updateById(userPunchClock);
		if (result) {
			responseHeaderModel.setCode(SysContants.SYS_SUCCESS_CODE);
			responseHeaderModel.setMessage("修改成功");
		} else {
			responseHeaderModel.setCode(SysContants.SYS_FAIL_CODE);
			responseHeaderModel.setMessage("修改失败");
		}
		return responseHeaderModel;
	}

	public ResponseModel<Page<UserPunchClock>> pageList(Pagination<UserPunchClock> page) {
		ResponseModel<Page<UserPunchClock>> responseModel = new ResponseModel<>();
		responseModel.setCode(SysContants.SYS_SUCCESS_CODE);
		responseModel.setMessage("查询成功");
		responseModel.setData(page(page, new QueryWrapper<UserPunchClock>(page.getEntity())));
		return responseModel;
	}
}
